/**

 ║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║
 ║║  ______           _                                                          _                _      ║║
 ║║ |  ____|         | |                               /\                       | |              | |     ║║
 ║║ | |__     _   _  | |_   _   _   _ __    ___       /  \     _ __    _ __     | |        __ _  | |__   ║║
 ║║ |  __|   | | | | | __| | | | | | '__|  / _ \     / /\ \   | '_ \  | '_ \    | |       / _` | | '_ \  ║║
 ║║ | |      | |_| | | |_  | |_| | | |    |  __/    / ____ \  | |_) | | |_) |   | |____  | (_| | | |_) | ║║
 ║║ |_|       \__,_|  \__|  \__,_| |_|     \___|   /_/    \_\ | .__/  | .__/    |______|  \__,_| |_.__/  ║║
 ║║                                                           | |     | |                                ║║
 ║║                                                           |_|     |_|                                ║║
 ║║                                                                                                      ║║
 ║║           ******************************************************************************             ║║
 ║║                                                                                                      ║║
 ║║                 ___                 _      _     ____  _                               _   _         ║║
 ║║                |   \   __ _  __ __ (_)  __| |   |_  / (_)  _ __    _ __    ___   _ _  | | (_)        ║║
 ║║                | |) | / _` | \ V / | | / _` |    / /  | | | '  \  | '  \  / -_) | '_| | | | |        ║║
 ║║                |___/  \__,_|  \_/  |_| \__,_|   /___| |_| |_|_|_| |_|_|_| \___| |_|   |_| |_|        ║║                                
 ║║                ______________________________________________________________________________        ║║
 ║║                                                                                                      ║║
 ║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║║
 

 */
package GameModel;

import java.util.Comparator;

import org.simpleframework.xml.Element;
/**
 * 
 * @author Christopher Vogel
 * @author David Zimmerli
 */
public class mCard implements Comparable<mCard> {
	public static boolean SortFirstByColor = true;
	@Element
	String cardName;
	@Element
	int cardNumber;
	@Element
	private eCardColor cardColor;
	@Element
	private int cardPoints;

	public mCard(@Element(name = "cardName") String cardName,
			@Element(name = "cardNumber") int cardNumber,
			@Element(name = "cardColor") eCardColor cardColor,
			@Element(name = "cardPoints") int cardPoints) {
		super();
		this.cardName = cardName;
		this.cardNumber = cardNumber;
		this.cardColor = cardColor;
		this.cardPoints = cardPoints;
	}

	public mCard(eCardColor cardColor, int cardNumber) {
		super();
		this.cardName = cardColor + "0" + cardNumber;
		this.cardNumber = cardNumber;
		this.cardColor = cardColor;
		if (cardColor != eCardColor.JOKER) {
			this.cardPoints = cardNumber % 2;
		} else {
			int[] jokerpoints = { 2, 3, 5 };
			String[] jokernames = { "jack", "queen", "king" };
			this.cardPoints = jokerpoints[cardNumber - 1];
			this.cardName = jokernames[cardNumber - 1];
		}

	}

	/**
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 * @return cardName
	 */
	@Override
	public String toString() {
		return "" + cardName + "";
	}

	public void setName(String cardName) {
		this.cardName = cardName;
	}

	public String getCardName() {

		return cardName;
	}

	public int getCardNumber() {

		return cardNumber;
	}

	public eCardColor getCardColor() {

		return cardColor;
	}

	public int getCardPoints() {
		return cardPoints;

	}

	/**
	 * Sorting the Cards after - Name and then Number - Number
	 * 
	 * 
	 */
	public static Comparator<mCard> cardNumberComparator = new Comparator<mCard>() {

		public int compare(mCard card1, mCard card2) {

			int cardNumber1 = card1.getCardNumber();
			int cardNumber2 = card2.getCardNumber();
			/* For ascending order */
			return cardNumber1 - cardNumber2;

			/* For descending order */
			// CardNumber2-CardNumber1;
		}
	};

	public boolean isSameTypeAndNumber(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		mCard other = (mCard) obj;
		if (cardColor == null) {
			if (other.cardColor != null)
				return false;
		} else if (cardColor != other.cardColor) {
			// Wenn string leer ist, ignoriere Farbentyp!!!
			if (other.cardColor != eCardColor.ALL) {
		//		System.out.println("nicht gleiche farbe: " + other.cardColor);
				return false;
			}
		}

		if (this.cardNumber != other.getCardNumber())
			return false;
		return true;

	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((cardColor == null) ? 0 : cardColor.hashCode());
		result = prime * result
				+ ((cardName == null) ? 0 : cardName.hashCode());
		result = prime * result + cardNumber;
		result = prime * result + cardPoints;
		return result;
	}

	public void setCardNumber(int cardNumber) {
		this.cardNumber = cardNumber;
	}

	public void setCardColor(eCardColor cardColor) {
		this.cardColor = cardColor;
	}

	/**
	 * Sortiert die Karten
	 * 
	 * 
	 */

	public static Comparator<mCard> cardNameComparator = new Comparator<mCard>() {

		public int compare(mCard card1, mCard card2) {
			String cardName1 = card1.getCardName();
			String cardName2 = card2.getCardName();
			// ascending order
			return cardName1.compareTo(cardName2);
			// descending order
			// return cardName2.compareTo(CardName1);
		}
	};

	@Override
	public int compareTo(mCard o) {
		int ForeignKey;
		int ThisKey;
		if (SortFirstByColor) {

			ForeignKey = o.getCardColor().getValue() * 100
					+ o.getCardNumber();
			ThisKey = this.getCardColor().getValue() * 100
					+ this.getCardNumber();
		} else {
			ForeignKey = o.getCardNumber() * 100
					+ o.getCardColor().getValue();
			ThisKey = this.getCardNumber() * 100
					+ this.getCardColor().getValue();
		}
		
		if (ThisKey < ForeignKey) {
			return -1;
		} else if (ThisKey == ForeignKey) {
			return 0;
		} else {
			return 1;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		mCard other = (mCard) obj;
		if (cardColor != other.cardColor)
			return false;
		if (cardName == null) {
			if (other.cardName != null)
				return false;
		} else if (!cardName.equals(other.cardName))
			return false;
		if (cardNumber != other.cardNumber)
			return false;
		if (cardPoints != other.cardPoints)
			return false;
		return true;
	}

}
